package com.xs.business.user.web;

import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;

import com.alibaba.fastjson.JSON;
import com.xs.business.user.entity.User;
import com.xs.business.user.service.UserService;
import com.xs.mvc.common.ResultWrapper;
import com.xs.mvc.web.BaseController;

/**
 * @Title: <P>UserController.java</P>
 * @Package <P>com.xs.business.user.web</P>
 * @Description: <P>BaseController</P>
 * @author renweiping
 * @date 2019年1月15日 下午4:01:08
 * @version 1.0
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/getAllUser.action")
	public ModelAndView  getAllUser( ){
		List<User> list= userService.getAll();
		ModelAndView v = new ModelAndView (new JsonView(list));
		return v;
	}
	
	
	@RequestMapping("/toPage.action")
	public String toPage(){
		return "/index";
	}
	
	
	@RequestMapping("/toErrorPage.action")
	public String toErrorPage(){
		int r = 1/0;
		return "/index";
	}
	
	@RequestMapping("/toErrorJson.action")
	public Object toErrorPageJson(){
		int r = 1/0;
		return new Object();
	}
	
	@RequestMapping(path="/save.action")
	@ResponseBody
	public Object save(){
		User u  = new User();
		u.setAge(20);
		u.setUserName("aa");
		userService.save(u);
		return "ok";
	}
	
	@RequestMapping(path="/redisLock.action")
	@ResponseBody
	public Object redisLock(  User user){
		ResultWrapper result = null;
		try {
			 result = this.userService.buyGoods(user);
			 Thread.sleep(new Random().nextInt(1000));
		} catch (Exception e) {
			result  = new ResultWrapper("请稍后再试"+e.getMessage());
		}
		LOG.info("秒杀结果为：" + JSON.toJSONString(result));
		return result;
	}
	
	
	@RequestMapping(path="/zkLock.action")
	@ResponseBody
	public Object zkLock( User user){
		ResultWrapper result = null;
		try {
			 result = this.userService.buyGoodsZk2(user);
			 Thread.sleep(new Random().nextInt(1000));
		} catch (Exception e) {
			result  = new ResultWrapper("请稍后再试"+e.getMessage());
			e.printStackTrace();
			LOG.error(e);
		}
		LOG.info("秒杀结果为：" + JSON.toJSONString(result));
		return result;
	}
	
	
	
	class JsonView implements View{

		private Object objectJson;

		public JsonView(Object objectJson) {
			super();
			this.objectJson = objectJson;
		}

		public Object getObjectJson() {
			return objectJson;
		}

		public void setObjectJson(Object objectJson) {
			this.objectJson = objectJson;
		}

		/* (non-Javadoc)
		 * @see org.springframework.web.servlet.View#getContentType()
		 */
		@Override
		public String getContentType() {
			// TODO Auto-generated method stub
			return "application/json;charset=UTF-8";
		}

		/* (non-Javadoc)
		 * @see org.springframework.web.servlet.View#render(java.util.Map, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
		 */
		@Override
		public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
				throws Exception {
			 response.setContentType(getContentType());
             ServletOutputStream out =   response.getOutputStream();
             try
             {
                 if(objectJson != null)
                 {
                     String json = JSON.toJSONString(objectJson);
                     byte[] bytes = json.getBytes("utf-8");
                     out.write(bytes );
                     out.flush();
                 }
             }finally
             {
                 if(out != null)
                 {
                     out.close();
                 }
             }
			
		}

	}
	
	
}
